package com.hy.service;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.mapper.AppDoctorMapper;
import com.hy.mapper.HealthVideoMapper;
import com.hy.pojo.doctor.AppDoctor;
import com.hy.pojo.healthvideo.HealthVideo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class HealthVideoServiceImpl implements HealthVideoService {
    @Autowired
    private HealthVideoMapper healthVideoMapper;

    @Autowired
    private AppDoctorMapper doctorMapper;


    // 获取视频列表
    @Override
    public PageResult<HealthVideo> getVideoList(Integer page, Integer pageSize, String title, String category) {
        // 使用 LambdaQueryWrapper 构造查询条件
        LambdaQueryWrapper<HealthVideo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HealthVideo::getStatus, 1)
                .eq(HealthVideo::getIsDeleted, false)
                .like(StringUtils.hasText(title), HealthVideo::getTitle, title)
                .eq(StringUtils.hasText(category), HealthVideo::getCategory, category)
                .orderByDesc(HealthVideo::getSort)
                .orderByDesc(HealthVideo::getCreateTime);

        // 分页查询
        Page<HealthVideo> pageInfo = new Page<>(page, pageSize);
        Page<HealthVideo> result = healthVideoMapper.selectPage(pageInfo, wrapper);

        // 填充医生信息
        if (result.getRecords() != null && !result.getRecords().isEmpty()) {
            // 获取所有视频关联的医生ID
            Set<Integer> doctorIds = result.getRecords().stream()
                    .map(HealthVideo::getDoctorId)
                    .collect(Collectors.toSet());

            // 打印医生ID，查看是否正确
//            System.out.println("医生ID集合：" + doctorIds);

            // 批量查询医生信息
            List<AppDoctor> doctors = doctorMapper.selectBatchIds(doctorIds);

            // 打印查询到的医生信息，查看是否有数据
//            System.out.println("查询到的医生信息：" + doctors);


            Map<Integer, AppDoctor> doctorMap = doctors.stream()
                    .collect(Collectors.toMap(AppDoctor::getId, Function.identity()));

            // 设置医生信息
            result.getRecords().forEach(video -> {
//                System.out.println("视频ID: " + video.getId() + "，医生ID: " + video.getDoctorId());
                video.setAppDoctor(doctorMap.get(video.getDoctorId()));
            });
        }

        // 创建PageResult对象并设置数据
        PageResult<HealthVideo> pageResult = new PageResult<>(page, pageSize);
        pageResult.setTotal((int) result.getTotal());
        pageResult.addAll(result.getRecords());
        return pageResult;
    }


//    @Override
//    public HealthVideo getVideoById(Long id) {
//        HealthVideo video = healthVideoMapper.selectById(id);
//        if (video != null && !video.getIsDeleted() && video.getStatus() == 1) {
//            AppDoctor doctor = doctorMapper.selectById(video.getDoctorId());
//            video.setDoctor(doctor);
//            return video;
//        }
//        return null;
//    }
//
//    @Override
//    @Transactional
//    public void incrementViewCount(Long id) {
//        // 使用乐观锁更新播放次数
//        HealthVideo video = healthVideoMapper.selectById(id);
//        if (video != null) {
//            video.setViewCount(video.getViewCount() + 1);
//            healthVideoMapper.updateById(video);
//        }
//    }
//
//    @Override
//    @Transactional
//    public void incrementLikeCount(Long id) {
//        // 使用乐观锁更新点赞数
//        HealthVideo video = healthVideoMapper.selectById(id);
//        if (video != null) {
//            video.setLikeCount(video.getLikeCount() + 1);
//            healthVideoMapper.updateById(video);
//        }
//    }
//}

}
